package org.rosildos.ricotta;

import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;

public abstract class AbstractHibernateTestCase {

	private static Logger logger = Logger.getLogger(AbstractHibernateTestCase.class);
	
	private static SessionFactory sessionFactory;

	protected Session session;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	final Configuration cfg = new Configuration();
	cfg.configure();
	sessionFactory = cfg.buildSessionFactory();		
	Assert.assertNotNull(sessionFactory);	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	public AbstractHibernateTestCase() {
		super();
	}

	@Before
	public void setUp() throws Exception {
		logger.info("Opening hibernate session...");
		session = sessionFactory.openSession();
		Assert.assertNotNull(session);
	}

	@After
	public void tearDown() throws Exception {
		logger.info("Closing hibernate session...");
		if (session != null) session.close();
	}

}